/*******************************************************************************
 * Copyright (c) Jonathan Buck - jonbuck16@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    jonbuck16@gmail.com - initial API and implementation
 *******************************************************************************/
package net.jonbuck.tassoo.ui.util;

import java.net.MalformedURLException;
import java.net.URL;

import net.jonbuck.tassoo.ui.Activator;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

/**
 * 
 * 
 * @since 1.0.0
 */
public abstract class CommonImages {

	public static final ImageDescriptor ADD = create("en16", "add.gif");

	public static final ImageDescriptor ARROW_DOWN = create("en16", "arrow_down.gif");

	public static final ImageDescriptor ARROW_ROTATE_ANTICLOCKWISE = create("en16", "arrow_rotate_anticlockwise.gif");
	
	public static final ImageDescriptor ARROW_UP = create("en16", "arrow_up.gif");
	
	public static final ImageDescriptor ATTACH = create("en16", "attach.gif");
	
	private static final URL baseURL = Activator.getDefault().getBundle().getEntry("/icons/"); //$NON-NLS-1$
	
	public static final ImageDescriptor BELL = create("bkgd", "bell.gif");
	
	public static final ImageDescriptor BGROUND_TOOLBAR = create("bkgd", "toolbar.gif");
	
	public static final ImageDescriptor CALENDAR = create("en16", "calendar.gif");
	
	public static final ImageDescriptor CHECK = create("en16", "check.gif");

	public static final ImageDescriptor CHECK_DISABLED = create("ds16", "check.gif");

	public static final ImageDescriptor COG_EDIT = create("en16", "cog_edit.gif");
	
	public static final ImageDescriptor COMMENT = create("en16", "comment.gif");
	
	public static final ImageDescriptor COMMENT_ADD = create("en16", "comment_add.gif");
	
	public static final ImageDescriptor CROSS = create("en16", "cross.gif");

	public static final ImageDescriptor CROSS_DISABLED = create("ds16", "cross.gif");
	
	public static final ImageDescriptor DELETE = create("en16", "delete.gif");
	
	public static final ImageDescriptor DELETE_DISABLED = create("ds16", "delete.gif");
	
	public static final ImageDescriptor EDIT = create("en16", "edit.gif");
	
	public static final ImageDescriptor EDIT_DISABLED = create("ds16", "edit.gif");
	
	public static final ImageDescriptor ERROR = create("en16", "error.gif");
	
	public static final ImageDescriptor FOLDER = create("en16", "folder.gif");
	
	public static final ImageDescriptor FONT = create("en16", "font.gif");
	
	public static final ImageDescriptor GTK_EDIT = create("en16", "gtk_edit.gif");
	
	private static ImageRegistry imageRegistry;
		
	public static final ImageDescriptor MAIL_ATTACHMENT = create("en16", "mail_attachment.gif");
	
	public static final ImageDescriptor MEDAL_GOLD = create("en16", "medal_gold.gif");
	
	public static final ImageDescriptor NOTE = create("en16", "note.gif");
	
	public static final ImageDescriptor PACKAGE_GO = create("en16", "package_go.gif");

	public static final ImageDescriptor PAGE = create("en16", "page.gif");
	
	public static final ImageDescriptor PAGE_EDIT = create("en16", "page_edit.gif");
	
	public static final ImageDescriptor PAGE_GREEN = create("en16", "page_green.gif");
	
	public static final ImageDescriptor PENCIL = create("en16", "pencil.gif");
	
	public static final ImageDescriptor PERCENTAGE = create("en16", "percentage.gif");
	
	public static final ImageDescriptor PLUS = create("en16", "plus.gif");
	
	public static final ImageDescriptor PLUS_DISABLED = create("ds16", "plus.gif");
	
	public static final ImageDescriptor PROCESS = create("en16", "process.gif");
	
	public static final ImageDescriptor PROCESS_DISABLED = create("ds16", "process.gif");
	
	public static final ImageDescriptor ROCKET_FLY = create("en16", "rocket_fly.gif");
	
	public static final ImageDescriptor TAG_PURPLE = create("en16", "tag_purple.gif");
	
	public static final ImageDescriptor TEXT_ALIGN_CENTER = create("en16", "text_align_center.gif");
	
	public static final ImageDescriptor TEXT_ALIGN_JUSTIFY = create("en16", "text_align_justify.gif");
	
	public static final ImageDescriptor TEXT_ALIGN_LEFT = create("en16", "text_align_left.gif");
	
	public static final ImageDescriptor TEXT_ALIGN_RIGHT = create("en16", "text_align_right.gif");
	
	public static final ImageDescriptor TEXT_ALLCAPS = create("en16", "text_allcaps.gif");
	
	public static final ImageDescriptor TEXT_BOLD = create("en16", "text_bold.gif");

	public static final ImageDescriptor TEXT_COLUMNS = create("en16", "text_columns.gif");

	public static final ImageDescriptor TEXT_DROPCAPS = create("en16", "text_dropcaps.gif");
	
	public static final ImageDescriptor TEXT_HEADING_1 = create("en16", "text_heading_1.gif");
	
	public static final ImageDescriptor TEXT_HEADING_2 = create("en16", "text_heading_2.gif");

	public static final ImageDescriptor TEXT_HEADING_3 = create("en16", "text_heading_3.gif");

	public static final ImageDescriptor TEXT_HEADING_4 = create("en16", "text_heading_4.gif");

	public static final ImageDescriptor TEXT_HEADING_5 = create("en16", "text_heading_5.gif");

	public static final ImageDescriptor TEXT_HEADING_6 = create("en16", "text_heading_6.gif");
	
	public static final ImageDescriptor TEXT_HORIZONTALRULE = create("en16", "text_horizontalrule.gif");
	
	public static final ImageDescriptor TEXT_INDENT = create("en16", "text_indent.gif");
	
	public static final ImageDescriptor TEXT_INDENT_REMOVE = create("en16", "text_indent_remove.gif");
	
	public static final ImageDescriptor TEXT_ITALIC = create("en16", "text_italic.gif");
	
	public static final ImageDescriptor TEXT_KERNING = create("en16", "text_kerning.gif");
	
	public static final ImageDescriptor TEXT_LETTER_OMEGA = create("en16", "text_letter_omega.gif");
	
	public static final ImageDescriptor TEXT_LETTERSPACING = create("en16", "text_letterspacing.gif");
	
	public static final ImageDescriptor TEXT_LINESPACING = create("en16", "text_linespacing.gif");
	
	public static final ImageDescriptor TEXT_LIST_BULLETS = create("en16", "text_list_bullets.gif");
	
	public static final ImageDescriptor TEXT_LIST_NUMBERS = create("en16", "text_list_numbers.gif");
	
	public static final ImageDescriptor TEXT_LOWERCASE = create("en16", "text_lowercase.gif");
	
	public static final ImageDescriptor TEXT_PADDING_BOTTOM = create("en16", "text_padding_bottom.gif");
	
	public static final ImageDescriptor TEXT_PADDING_LEFT = create("en16", "text_padding_left.gif");
	
	public static final ImageDescriptor TEXT_PADDING_RIGHT = create("en16", "text_padding_right.gif");
	
	public static final ImageDescriptor TEXT_PADDING_TOP = create("en16", "text_padding_top.gif");
	
	public static final ImageDescriptor TEXT_REPLACE = create("en16", "text_replace.gif");
	
	public static final ImageDescriptor TEXT_SIGNATURE = create("en16", "text_signature.gif");
	
	public static final ImageDescriptor TEXT_SMALLCAPS = create("en16", "text_smallcaps.gif");
	
	public static final ImageDescriptor TEXT_STRIKETHROUGH = create("en16", "text_strikethrough.gif");
	
	public static final ImageDescriptor TEXT_SUBSCRIPT = create("en16", "text_subscript.gif");
	
	public static final ImageDescriptor TEXT_SUPERSCRIPT = create("en16", "text_superscript.gif");
	
	public static final ImageDescriptor TEXT_UNDERLINE = create("en16", "text_underline.gif");
	
	public static final ImageDescriptor TEXT_UPPERCASE = create("en16", "text_uppercase.gif");
	
	public static final ImageDescriptor TIMELINE_MARKER = create("en16", "timeline_marker.gif");
	
	public static final ImageDescriptor TRAFFIC_LIGHT = create("en16", "traffic_light.gif");
	
	public static final ImageDescriptor VEGASTRIKE = create("en16", "vegastrike.gif");

	public static final ImageDescriptor WIZBAN_ATTACHMENT = create("wizban", "attachment.gif");
	
	public static final ImageDescriptor WIZBAN_CATEGORY = create("wizban", "category.gif");

	public static final ImageDescriptor WIZBAN_PRIORITY = create("wizban", "priority.gif");

	public static final ImageDescriptor WIZBAN_STATUS = create("wizban", "status.gif");

	public static final ImageDescriptor WIZBAN_TYPE = create("wizban", "type.gif");
	
	/**
	 * 
	 * @param prefix
	 * @param name
	 * @return
	 */
	private static ImageDescriptor create(final String prefix, final String name) {
		try {
			return ImageDescriptor.createFromURL(makeIconFileURL(prefix, name));
		} catch (MalformedURLException e) {
			return ImageDescriptor.getMissingImageDescriptor();
		}
	}

	/**
	 * Lazily initialises image map.
	 * 
	 * @param imageDescriptor
	 *            The Image descriptor as created within this class.
	 * @return Image the image from the Image Registry
	 */
	public static Image getImage(final ImageDescriptor imageDescriptor) {
		ImageRegistry imageRegistry = getImageRegistry();
		Image image = imageRegistry.get("" + imageDescriptor.hashCode()); //$NON-NLS-1$
		if (image == null) {
			ImageData currentImage = imageDescriptor.getImageData();
			int whitePixel = currentImage.palette.getPixel(new RGB(255, 255, 255));
			currentImage.transparentPixel = whitePixel;
			image = new Image(Display.getDefault(), currentImage, currentImage.getTransparencyMask());
			imageRegistry.put("" + imageDescriptor.hashCode(), image); //$NON-NLS-1$
		}
		return image;
	}

	/**
	 * Creates a new Image Registry if the internal instance does not exist.
	 * 
	 * @return imageRegistry the internal instance of the image registry.
	 */
	private static ImageRegistry getImageRegistry() {
		if (CommonImages.imageRegistry == null) {
			CommonImages.imageRegistry = new ImageRegistry();
		}
		return CommonImages.imageRegistry;
	}

	/**
	 * 
	 * @param prefix
	 * @param name
	 * @return
	 * @throws MalformedURLException
	 */
	private static URL makeIconFileURL(final String prefix, final String name) throws MalformedURLException {
		if (CommonImages.baseURL == null) {
			throw new MalformedURLException();
		}
		StringBuffer buffer = new StringBuffer(prefix);
		buffer.append('/');
		buffer.append(name);
		return new URL(CommonImages.baseURL, buffer.toString());
	}

}
